
    import { createRouter, createWebHashHistory } from 'vue-router';
    import axios from "axios";

    const router = createRouter({
    history: createWebHashHistory(),
    routes: [
      {
        path: '/',
        redirect: '/daohang_ye'
      },
  {
    path: "/gongsi_yunying_wendingxing",
    name: "公司运营稳定性",
    component: () => import("./公司运营稳定性.vue")
  },
  {
    path: "/gongsi_yewu_leixing",
    name: "公司业务类型",
    component: () => import("./公司业务类型.vue")
  },
  {
    path: "/pingfen_guanli",
    name: "评分管理",
    component: () => import("./评分管理.vue")
  },
  {
    path: "/xiangmu_yunxing_baobiao",
    name: "项目运行报表",
    component: () => import("./项目运行报表.vue")
  },
  {
    path: "/xiangmu_jingji_baobiao",
    name: "项目经济报表",
    component: () => import("./项目经济报表.vue")
  },
  {
    path: "/tingji_biao",
    name: "停机表",
    component: () => import("./停机表.vue")
  },
  {
    path: "/juese_guanli",
    name: "角色管理",
    component: () => import("./角色管理.vue")
  },
  {
    path: "/daohang_ye",
    name: "导航页面",
    component: () => import("./导航页面.vue"),
    children: [
      {
        path: "",
        redirect: "/daohang_ye/xinxi_zonglan"
      },
      {
        path: "gongsi_yunying_wendingxing",
        name: "公司运营稳定性_child",
        components: {
          second_route: () => import("./公司运营稳定性.vue")
        }
      },
      {
        path: "gongsi_yewu_leixing",
        name: "公司业务类型_child",
        components: {
          second_route: () => import("./公司业务类型.vue")
        }
      },
      {
        path: "pingfen_guanli",
        name: "评分管理_child",
        components: {
          second_route: () => import("./评分管理.vue")
        }
      },
      {
        path: "xiangmu_yunxing_baobiao",
        name: "项目运行报表_child",
        components: {
          second_route: () => import("./项目运行报表.vue")
        }
      },
      {
        path: "xiangmu_jingji_baobiao",
        name: "项目经济报表_child",
        components: {
          second_route: () => import("./项目经济报表.vue")
        }
      },
      {
        path: "tingji_biao",
        name: "停机表_child",
        components: {
          second_route: () => import("./停机表.vue")
        }
      },
      {
        path: "juese_guanli",
        name: "角色管理_child",
        components: {
          second_route: () => import("./角色管理.vue")
        }
      },
      {
        path: "renyuan_paiban",
        name: "人员排班_child",
        components: {
          second_route: () => import("./人员排班.vue")
        }
      },
      {
        path: "xinxi_zonglan",
        name: "信息总览_child",
        components: {
          second_route: () => import("./信息总览.vue")
        }
      },
      {
        path: "beijian_guanli",
        name: "备件管理_child",
        components: {
          second_route: () => import("./备件管理.vue")
        }
      },
      {
        path: "lvse_gongxianzhi",
        name: "绿色贡献值_child",
        components: {
          second_route: () => import("./绿色贡献值.vue")
        }
      },
      {
        path: "gaojing_guize",
        name: "告警规则_child",
        components: {
          second_route: () => import("./告警规则.vue")
        }
      },
      {
        path: "kongbai_ye",
        name: "空白页面_child",
        components: {
          second_route: () => import("./空白页面.vue")
        }
      },
      {
        path: "yonghu_guanli",
        name: "用户管理_child",
        components: {
          second_route: () => import("./用户管理.vue")
        }
      },
      {
        path: "guzhang_liebiao",
        name: "故障列表_child",
        components: {
          second_route: () => import("./故障列表.vue")
        }
      },
      {
        path: "yingshou_shuju_shangbao",
        name: "营收数据上报_child",
        components: {
          second_route: () => import("./营收数据上报.vue")
        }
      },
      {
        path: "gaojing_liebiao",
        name: "告警列表_child",
        components: {
          second_route: () => import("./告警列表.vue")
        }
      },
      {
        path: "denglu_ye",
        name: "登录页_child",
        components: {
          second_route: () => import("./登录页.vue")
        }
      }
    ]
  },
  {
    path: "/renyuan_paiban",
    name: "人员排班",
    component: () => import("./人员排班.vue")
  },
  {
    path: "/xinxi_zonglan",
    name: "信息总览",
    component: () => import("./信息总览.vue")
  },
  {
    path: "/beijian_guanli",
    name: "备件管理",
    component: () => import("./备件管理.vue")
  },
  {
    path: "/lvse_gongxianzhi",
    name: "绿色贡献值",
    component: () => import("./绿色贡献值.vue")
  },
  {
    path: "/gaojing_guize",
    name: "告警规则",
    component: () => import("./告警规则.vue")
  },
  {
    path: "/kongbai_ye",
    name: "空白页面",
    component: () => import("./空白页面.vue")
  },
  {
    path: "/yonghu_guanli",
    name: "用户管理",
    component: () => import("./用户管理.vue")
  },
  {
    path: "/guzhang_liebiao",
    name: "故障列表",
    component: () => import("./故障列表.vue")
  },
  {
    path: "/yingshou_shuju_shangbao",
    name: "营收数据上报",
    component: () => import("./营收数据上报.vue")
  },
  {
    path: "/gaojing_liebiao",
    name: "告警列表",
    component: () => import("./告警列表.vue")
  },
  {
    path: "/denglu_ye",
    name: "登录页",
    component: () => import("./登录页.vue")
  }
]
    });

    
    async function isLoggedIn() {
    try {
        // 假设后端提供了一个名为 `./system/logincheck` 的接口
        const response = await axios.get('./system/logincheck');

        // const data = await response.json();
        // 假设后端返回的是一个对象，表示用户是否登录
        return response.data.data;
    } catch (error) {
        console.error('Error checking login status:', error);
        return false; // 在出错时默认返回未登录状态
    }
    }
    // 添加全局前置守卫
    router.beforeEach(async (to, from, next) => {
    const isLoggedInAsync = await isLoggedIn();
    if (!isLoggedInAsync && to.path !== '/denglu_ye') {
      debugger
        // 如果用户未登录且目标路由不是登录页，重定向到登录页
        next('/denglu_ye');
    } else {
        // 否则正常导航
        next();
    }
    });
    export default router;
    